package org.myosw.solr;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;

public class SolrUtils {

	/**
	 * @param args
	 * @throws SolrServerException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public static void main(String[] args) {
		PageUtil page = query("天融信", 1, 10);
		System.out.println(page.getTotalpage());
	}
	
	/**
	 * 
	 * @param keyword
	 * @param page
	 * @param size
	 * @return
	 */
	public static PageUtil query(String keyword,int page,int size) 
	{
		
		try {
		//module这个文件会自动在下面几个地方被搜寻：当前目录 、用户主目录 、classpath
		PropertiesConfiguration conf = new PropertiesConfiguration("system.properties");
			String solrUrl = conf.getString("solrUrl");
		
		
		
		HttpSolrServer server = new HttpSolrServer(solrUrl );
		server.setMaxRetries(1); // defaults to 0.  > 1 not recommended.
		server.setConnectionTimeout(5000); // 5 seconds to establish TCP
		server.setParser(new XMLResponseParser()); // binary parser is used by default
		server.setSoTimeout(1000);  // socket read timeout
		server.setDefaultMaxConnectionsPerHost(100);
		server.setMaxTotalConnections(100);
		server.setFollowRedirects(false);  // defaults to false
		server.setAllowCompression(true);
		SolrQuery query = new SolrQuery();
		query.setQuery(keyword);
		size = size>0?size:Integer.MAX_VALUE;
		query.setStart((page-1)*size);//设置起始条数
		query.setRows(size);// 设置每页显示条数
		

	
			QueryResponse  response = server.query(query);
			SolrDocumentList docs = response.getResults();
			long total =docs.getNumFound();//查出总数
			int time = response.getQTime();//时间
			List<PageInfo> list = new ArrayList<PageInfo>();
			for (SolrDocument doc : docs) { 
				
				
				Map<String, Object> mapdoc = doc.getFieldValueMap();
				
					Map<String, Object> map = new HashMap<String, Object>();
					
					
					map.put("id", mapdoc.get("id"));
					map.put("title", mapdoc.get("title"));
					map.put("content", mapdoc.get("content"));
					map.put("url", mapdoc.get("url"));
					map.put("_version_", mapdoc.get("_version_"));
					map.put("tstamp", mapdoc.get("tstamp"));
			//		map.put("anchor", mapdoc.get("anchor"));
					map.put("boost", mapdoc.get("boost"));
					map.put("digest", mapdoc.get("digest"));
					
					
					PageInfo bean = new PageInfo();
					BeanUtils.populate(bean, map);
					list.add(bean);
			}
			return new PageUtil(page, size, total, time, list);
			
		} catch (SolrServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return null;
		
		
		
	}

}
